﻿#region FileHeader
//////////////////////////////////////////////////////////////////////////////////
// Author:                          Hector Llanos
// Date:                            08.27.2010
// FileName:                        CHudState.cs
// Reason:                          HUD state for the game
//////////////////////////////////////////////////////////////////////////////////
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

using Run_Game;
using Globals;

namespace GameState
{
    public class CHudState : IGameState
    {
        private ContentManager m_pContent;
        private SpriteBatch m_pSpriteBatch;

        //Mr bang health
        private Rectangle m_rPlayer1Health;

        //miss pew health
        private Rectangle m_rPlayer2Health;
        
        //health bar texture
        private Texture2D square;

        //alpha the health bars out
        private float m_fPlayer1AlphaValue;
        private float m_fPlayer2AlphaValue;

        //health triggers
        private bool m_bPlayer1HealthTrigger;
        private bool m_bPlayer2HealthTrigger;
        
        //the elapsed time before fading in and out
        private float m_fPlayer1ElapsedHealth;
        private float m_fPlayer2ElapsedHealth;

        public bool Player2HealthTrigger
        {
            get { return m_bPlayer2HealthTrigger; }
            set { m_bPlayer2HealthTrigger = value; }
        }
        public bool Player1HealthTrigger
        {
            get { return m_bPlayer1HealthTrigger; }
            set { m_bPlayer1HealthTrigger = value; }
        }
       public int Player1Health
        {
            get { return m_rPlayer1Health.Width; }
            set { m_rPlayer1Health.Width = value; }
        }
        public int Player2Health
        {
            get { return m_rPlayer2Health.Width; }
            set { m_rPlayer2Health.Width = value; }
        }

        public void Enter(ContentManager content, SpriteBatch spriteBatch)
        {
            m_pContent = content;
            m_pSpriteBatch = spriteBatch;

            m_rPlayer1Health = new Rectangle(1, 1, 100, 25);
            m_rPlayer2Health = new Rectangle(1, 1, 100, 25);

            square = content.Load<Texture2D>("square");

            m_fPlayer1AlphaValue = 0f;
            m_fPlayer2AlphaValue = 0f;

            m_bPlayer1HealthTrigger = false;
            m_bPlayer2HealthTrigger = false;
        }
        public void Input()
        {
        
        }
        public void Update(TimeSpan fElapsedTime)
        {
            //if the health changed and we need show the health
            //slowly fade it in, whoever it may be
            if (m_bPlayer2HealthTrigger)
            {
                //wait two seconds before fading out
                m_fPlayer2ElapsedHealth += (float)fElapsedTime.TotalSeconds * 2.0f;
                m_fPlayer2AlphaValue += (float)fElapsedTime.TotalSeconds * 2.0f;

                if (m_fPlayer2ElapsedHealth > 2f)
                {
                    m_fPlayer2AlphaValue = 1f;
                    m_bPlayer2HealthTrigger = false;
                }
            }
            else // if(m_bPlayer2HealthTrigger == false)
            {
                //just stay a zero
                m_fPlayer2ElapsedHealth -= (float)fElapsedTime.TotalSeconds * 2.0f;
                m_fPlayer2AlphaValue -= (float)fElapsedTime.TotalSeconds * 2.0f;

                if (m_fPlayer2ElapsedHealth < 0f)
                {
                    m_fPlayer2AlphaValue = 0f;
                    m_bPlayer2HealthTrigger = false;
                }
            }

            if (m_bPlayer1HealthTrigger)
            {
                //wait two seconds before fading out
                m_fPlayer1ElapsedHealth += (float)fElapsedTime.TotalSeconds * 2.0f;
                m_fPlayer1AlphaValue += (float)fElapsedTime.TotalSeconds * 2.0f;

                if (m_fPlayer1ElapsedHealth > 2f)
                {
                    m_fPlayer1AlphaValue = 1f;
                    m_bPlayer1HealthTrigger = false;
                }
            }
            else //if (m_bPlayer1HealthTrigger == false)
            {
                //just stay a zero
                m_fPlayer1ElapsedHealth -= (float)fElapsedTime.TotalSeconds * 2.0f;
                m_fPlayer1AlphaValue -= (float)fElapsedTime.TotalSeconds * 2.0f;

                if (m_fPlayer1ElapsedHealth < 0f)
                {
                    m_fPlayer1AlphaValue = 0f;
                    m_bPlayer1HealthTrigger = false;
                }
            }
        }
        public void Render()
        {
            //draw mr bang's health
          //  m_pSpriteBatch.Draw(square, new Vector2(200, 50), Player1Health, new Color(255f, 0f, 0f, m_fPlayer1AlphaValue * 255.0f));

            m_pSpriteBatch.Draw(square, new Vector2(200, 50), m_rPlayer1Health, Color.Red * m_fPlayer1AlphaValue);

            //draw Miss Pew health
            m_pSpriteBatch.Draw(square, new Vector2(CGlobal.Screenwidth - 200, 50), m_rPlayer2Health, Color.Red * m_fPlayer2AlphaValue);

        }
        public void Exit()
        {
            m_fPlayer1AlphaValue = 0f;
            m_fPlayer2AlphaValue = 0f;
        }
    }
}
